package com.woniuxy.ssm.powermag.mapper;

import com.woniuxy.ssm.entity.DeptEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.Date;
import java.util.List;

public interface DeptMapper {

    @Options(useGeneratedKeys = true,keyProperty = "d.id")
    @Insert("insert into dept_info (dept_name,dept_desc,create_time,pid)" +
            " values(#{d.deptName},#{d.deptDesc},now(),#{d.parents.id})")
    void save(@Param("d") DeptEntity deptEntity);


    void update(@Param("d")DeptEntity deptEntity);


    @Delete("delete from dept_info where id = #{id}")
    void deleteById(Long id);


    @Results({
            @Result(id=true,property = "id",column = "id",javaType = Long.class),
            @Result(property = "deptName",column = "dept_name",javaType = String.class),
            @Result(property = "deptDesc",column = "dept_desc",javaType = String.class),
            @Result(property = "createTime",column = "create_time",javaType = Date.class),
            @Result(property = "parents",javaType = DeptEntity.class,column = "pid",
                    one = @One(fetchType = FetchType.LAZY,select = "getByPId"))
    })
    @Select("select * from dept_info where id = #{id}")
    DeptEntity getById(Long id);


    @ResultType(DeptEntity.class)
    @Select("select id,dept_name as deptName from dept_info where id = #{id}")
    DeptEntity getByPId(Long id);

    List<DeptEntity> page(@Param("d") DeptEntity deptEntity);
}
